package castledefense.game.render;

import java.util.ArrayList;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import castledefense.data.GameData;
import castledefense.data.Resources;
import castledefense.logic.Map;
import castledefense.logic.units.Unit;
import castledefense.logic.units.MiniBoss;

public class RenderUnit {

	public static void draw(Map map, SpriteBatch batch) {
		final ArrayList<Unit> list = map.getUnitList();
		final int size = list.size();

		for (int i = 0; i < size; i++) {
			final Unit unit = list.get(i);
			
			int width = Resources.fireUnitIm.getWidth();
			int height = Resources.fireUnitIm.getHeight();
			if(unit instanceof MiniBoss){
				width = width * 3;
				height = height * 3;
			}
			final float posX = unit.getRenderingX();
			final float posY = unit.getRenderingY();

			if (unit.getType() == GameData.fire) {
				batch.draw(Resources.fireUnitIm, posX, posY, width, height);
			} else if (unit.getType() == GameData.water) {
				batch.draw(Resources.waterUnitIm, posX, posY, width, height);
			} else if (unit.getType() == GameData.earth) {
				batch.draw(Resources.earthUnitIm, posX, posY, width, height);
			} else if (unit.getType() == GameData.air) {
				batch.draw(Resources.airUnitIm, posX, posY, width, height);
			}

			if (unit.isSelected()) {
				final float sizes = unit.getRenderingSize()*1.5f;
				if(unit instanceof MiniBoss){
					batch.draw(Resources.selectedIm, posX - 11, posY - 4, sizes,
							sizes);
				}else{
					batch.draw(Resources.selectedIm, posX - 4, posY - 4, sizes,
							sizes);
				}
			}
		}

		for (int i = 0; i < size; i++) {
			final Unit unit = list.get(i);
			if (unit.getHealth() > 0) {
				final float r = (float) ((unit.getInitialHealth() - unit
						.getHealth()) / unit.getInitialHealth());
				final float g = (float) (unit.getHealth() / unit
						.getInitialHealth());
				batch.setColor(r, g, 0f, 0.75f);
				final float w =  (float)(Map.cellWidth*(unit.getHealth() / unit.getInitialHealth()));
				
				if(unit instanceof MiniBoss){
					batch.draw(Resources.pixel, unit.getRenderingCenterX() - w * 2.3f / 2f, 
							unit.getRenderingCenterY() + unit.getRenderingSize()*0.75f
							, w*2.3f, -8f);
				}else{
					batch.draw(Resources.pixel, unit.getRenderingCenterX() - w
							/ 2f, unit.getRenderingCenterY() + unit.getRenderingSize()
							, w, -8f);
				}

			}
			batch.setColor(1f, 1f, 1f, 1f);
		}
	}

}
